{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f896d9da-105b-43af-8cb1-86dbe35c7051",
   "metadata": {},
   "outputs": [],
   "source": [
    "# llm的流式输出\n",
    "from langchain_openai import OpenAI\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"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\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://ai-yyds.com/v1\"\n",
    "\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "llm = OpenAI(\n",
    "    model= \"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens=512,\n",
    ")\n",
    "\n",
    "for chunk in llm.stream(\"写一首关于冬天的诗歌\"):\n",
    "    print(chunk, end=\"\", flush=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0656bac5-dcb1-43c5-85b0-8610a9308324",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install anthropic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07e9238d-75d0-4e5e-93be-cd3ae8d2d00c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# chatmodels的流式调用方法\n",
    "# 使用clade模型\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "import os\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"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\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://ai-yyds.com/v1\"\n",
    "\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    model = \"claude-3-opus-20240229\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens=512,\n",
    ")\n",
    "for chunk in llm.stream(\"写一首关于秋天的诗歌\"):\n",
    "    print(chunk,end=\"\\n\",flush=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (langchain)",
   "language": "python",
   "name": "langchain-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
